package com.songcha.sakura.mvp.model


import com.songcha.sakura.bean.ComicDetailBean
import com.songcha.sakura.config.BaseConfig
import com.songcha.sakura.mvp.contract.ComicDetailContract
import com.songcha.sakura.util.UrlUtil
import org.json.JSONObject
import org.jsoup.Jsoup
import javax.inject.Inject


class ComicDetailModel @Inject constructor(): ComicDetailContract.Model {
    override fun getDoubanScore(key: String):String {
        try{
            val document = Jsoup.connect("${BaseConfig.BASE_URL_DOUBAN}/search/?query=${UrlUtil.urlEncoded(key)}").get()
            val info=document.getElementsByClass("rating")[0]
            var score=info.child(info.childrenSize()-1).text()
            if(score.isBlank()) score="无"
            return score
        }catch (e:Exception){
            e.printStackTrace()
            return "无"
        }
    }

    override fun post(json: JSONObject?):ComicDetailBean{
         var href = ""
         if (json != null) {
             if (json.has("href"))
                 href = json.getString("href")

         }

         val document = Jsoup.connect("${BaseConfig.BASE_URL}$href").get()

         val xs = document.getElementsByClass("col-xs-1")[2]

         val thumb = xs.getElementsByClass("lazyload").attr("data-original")

         val content = xs.getElementsByClass("myui-content__detail")[0]

         val title = content.getElementsByClass("title")[0].text()
         val score = content.getElementsByClass("branch")[0].text()
         val cate = content.selectFirst("p[class=data]")!!.getElementsByTag("a")[0].text()
         val area = content.selectFirst("p[class=data]")!!.getElementsByTag("a")[1].text()
         val year = content.selectFirst("p[class=data]")!!.getElementsByTag("a")[2].text()
         val update = content.select("p[class=data hidden-sm] span[class=text-red]").text()

         val actorsElement = content.select("p[class=data]")[1].getElementsByTag("a")
         val actors = arrayListOf<String>()
         if (actorsElement.size > 0) {
             for (e in actorsElement) {
                 actors.add(e.text())
             }
         } else {
             actors.add("未知")
         }

         val directorElement = content.select("p[class=data]")[2].getElementsByTag("a")
         var director = "未知"
         if (directorElement.size > 0) {
             director = directorElement[0].text()
         }

         //var brief = content.select("p[class=data hidden-xs]").text()
         //brief = brief.substring(3, brief.length - 2)

         val brief=document.getElementsByClass("sketch content").text()

         var briefAll=""
         try{
             val briefData=document.select("div[class=col-pd text-collapse content]")[0].selectFirst("span[class=data]")!!
             if(briefData.getElementsByTag("p").size>0){
                 briefAll=briefData.getElementsByTag("p").text()
             }else{
                 briefAll=briefData.text()
             }
         }catch (e:Exception){
             e.printStackTrace()
         }


         val play_source_element =
             document.getElementsByClass("nav nav-tabs active")[0].getElementsByTag("li")
         val play_source = arrayListOf<String>()
         for (e in play_source_element) {
             play_source.add(e.text())
         }

         val play_url_list_element =
             document.getElementsByClass("tab-content myui-panel_bd")[0].getElementsByTag("ul")

         val playurlList: MutableList<MutableList<ComicDetailBean.EpisodeBean>> = mutableListOf()

         for (e in play_url_list_element) {
             val lie = e.getElementsByTag("li")
             val list: MutableList<ComicDetailBean.EpisodeBean> = mutableListOf()
             for (e2 in lie) {
                 val a = e2.getElementsByTag("a")[0]
                 val episode = ComicDetailBean.EpisodeBean(a.text(), a.attr("href"))
                 list.add(episode)
             }
             playurlList.add(list)
         }


         return ComicDetailBean(
             thumb,
             title,
             score,
             cate,
             area,
             year,
             update,
             actors,
             director,
             brief,
             briefAll,
             play_source,
             playurlList,
             ""
         )
     }
 }